package com.dwms.controller;

import com.dwms.feign.system.UserFeignClient;
import com.dwms.entity.*;
import com.dwms.entity.system.user.request.ModifyPassword;
import com.dwms.entity.system.user.request.UserQueryRequest;
import com.dwms.entity.system.user.request.UserRequest;
import com.dwms.entity.system.user.response.UserResponse;
import com.dwms.rsa.RSAService;
import com.dwms.utils.WebUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @ClassName: UserMapper
 * @Description: User
 * @Create 2022/02/27 17:44
 * @Author WH
 * @Since 2.0.0
**/
@Tag(name = "用户管理")
@RestController(value = "UserController")
@RequestMapping(value = "/system/user")
@RequiredArgsConstructor
public class UserController {

    private final HttpServletRequest request;
    private final UserFeignClient userFeignClient;
    private final RSAService rsaService;

    @Operation(summary = "新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody UserRequest userRequest) {
        WebUtil.getAdminRequest(request,userRequest);
        String password = rsaService.decrypt(userRequest.getPassword(), userRequest.getSessionGenerateId());
        userRequest.setPassword(password);
        return userFeignClient.save(userRequest);
    }

    @Operation(summary = "修改用户")
    @PostMapping("/update")
    public Result update(@RequestBody UserRequest userRequest) {
        WebUtil.getAdminRequest(request,userRequest);
        return userFeignClient.update(userRequest);
    }

    @Operation(summary = "分页查询用户")
    @PostMapping("/findPage")
    public Result<PageResponse<UserResponse>> findPage(@RequestBody UserQueryRequest query) {
        WebUtil.getAdminRequest(request,query);
        return userFeignClient.findPage(query);
    }

    @Operation(summary = "根据ID查询")
    @PostMapping("/findById")
    public Result findById(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request,query);
        return userFeignClient.findById(query);
    }

    @Operation(summary = "删除用户")
    @PostMapping("/delete")
    public Result delete(@RequestBody QueryRequest query){
        WebUtil.getAdminRequest(request,query);
        return userFeignClient.delete(query);
    }

    @Operation(summary = "启用/禁用")
    @PostMapping("/enabled")
    public Result enabled(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request,query);
        return userFeignClient.enabled(query);
    }

    @Operation(summary = "解锁/锁定")
    @PostMapping("/locked")
    public Result locked(@RequestBody QueryRequest query) {
        WebUtil.getAdminRequest(request,query);
        return userFeignClient.locked(query);
    }


    @Operation(summary = "修改密码")
    @PostMapping("/modifyPassword")
    public Result modifyPassword(@RequestBody ModifyPassword modifyPassword) {
        WebUtil.getAdminRequest(request,modifyPassword);
        String password = rsaService.decrypt(modifyPassword.getPassword(), modifyPassword.getSessionGenerateId());
        modifyPassword.setPassword(password);
        return userFeignClient.modifyPassword(modifyPassword);
    }
}
